const SaleOrderLine = {
  // configs: {},
  _description: '销售订单明细',
  _fields: {
    product_id: {
      type: 'many2one',
      string: '产品',
      fields: {
        name: null,
        list_price: null
      }
    },
    product_uom_qty: { type: 'float', string: '数量' },
    price_unit: { type: 'monetary', string: '单价' },
    price_subtotal: { type: 'monetary', string: '小计', compute: true }
  },

  metadata: {
    onchange: ({ field, value, record, options }) => {
      // console.log(' sol, onchange, ', field, value, record, options)
      if (field === 'product_id') {
        const ops = options.filter(item => item.value === value)
        if (ops.length) {
          const ref = ops[0].object
          return { price_unit: ref.list_price }
        }
      }

      return {}
    }
  }
}

const SaleOrder = {
  // configs: {},
  _description: '销售订单',
  _fields: {
    state: { selection: {}, type: 'selection', string: '状态' },
    invoice_status: { selection: {}, type: 'selection', string: '开票状态' },

    name: { type: 'char', string: '编号' },
    partner_id: { type: 'many2one', string: '客户' },
    date_order: { type: 'datetime', string: '订单日期' },
    order_line: {
      type: 'one2many',
      relation: 'sale.order.line',
      string: '明细'
    },
    amount_total: { type: 'monetary', string: '总金额', compute: true },
    note: { type: 'text', string: '备注' }
  },

  metadata: {
    help: '参与人页面',

    fieldsForBrowse: {
      state: null,
      invoice_status: null,
      name: null,
      partner_id: null,
      date_order: null,
      order_line: {
        product_id: null,
        product_uom_qty: null,
        price_unit: null,
        price_subtotal: null
      },
      amount_total: null,
      note: null
    },

    propForView: {
      workflow: {
        state: rec => {
          // console.log('fn, state,', rec.state)
          // draft, sent,
          // sale, to invoice, invoiced
          // done, cancel,

          if (rec.state !== 'sale') {
            return rec.state
          } else {
            if (rec.invoice_status === 'to invoice') {
              return rec.invoice_status
            } else if (rec.invoice_status === 'invoiced') {
              return rec.invoice_status
            } else {
              return rec.state
            }
          }
        },

        steps: {
          draft: { string: '草稿' }, // 总是显示,
          sent: { string: '已发送', states: ['sent'] }, // 仅在 发送后显示,
          sale: { string: '确认', invisible: ['to invoice'] }, // 待开票时 不显示,
          'to invoice': { string: '确认', states: ['to invoice'] }, // 仅在 待开票时  显示,
          invoiced: { string: '开票', states: ['to invoice', 'invoiced'] }, // 需要开票时显示,  已经开票后显示,
          done: { string: '锁定', states: ['done'] }, // 仅在 锁定后显示
          cancel: { string: '取消', states: ['cancel'] } // 仅在 取消后显示

          //
          // ('draft', 'Quotation'),
          // ('sent', 'Quotation Sent'),
          // ('sale', 'Sales Order'),
          // ('done', 'Locked'),
          // ('cancel', 'Cancelled'),

          // invoice_status = fields.Selection([
          //   ('upselling', 'Upselling Opportunity'),
          //   ('invoiced', 'Fully Invoiced'),
          //   ('to invoice', 'To Invoice'),
          //   ('no', 'Nothing to Invoice')
          //   ], string='Invoice Status', compute='_get_invoiced', store=True, readonly=True)
        },

        buttons: {
          action_confirm: {
            type: 'object',
            string: '确认',
            message: '确认订单?',
            btn_type: 'primary',
            states: ['draft', 'sent']
          },
          create_invoice: {
            type: 'action',
            string: '开票',
            message: '确认要开票?',
            btn_type: 'primary',
            states: ['to invoice'],
            wizard: {
              submit: true,
              model: 'sale.advance.payment.inv',
              vals: {},
              context: {},
              fields: {},
              method: 'create_invoices'
            }
          },
          action_cancel: {
            type: 'object',
            string: '取消',
            message: '取消订单?',
            states: ['draft', 'sent', 'sale']
          },
          action_draft: {
            type: 'object',
            string: '重置为草稿',
            states: ['cancel']
          }
        }
      },

      fields: {
        // invoice_status: null,
        name: null,
        partner_id: { popupselected: true },
        date_order: null
      },

      notebook: {
        default: 'order_lines',
        pages: [
          {
            name: 'order_lines',
            label: '订单行',
            fields: {
              //
              order_line: {
                propForView: {
                  fields: {
                    product_id: { popupselected: true },
                    product_uom_qty: null,
                    price_unit: null,
                    price_subtotal: null
                  }
                },

                propForList: {
                  title: rec => {
                    return `${rec.product_id__name}`
                  },
                  icon: rec => {
                    return ''
                  },

                  label: rec => {
                    return ``
                  },
                  value: rec => {
                    return `${rec.product_uom_qty} * ¥${
                      rec.price_unit
                    } = ¥${rec.product_uom_qty * rec.price_unit}`
                  }
                }
              },
              amount_total: null,
              note: null
            }
          }
        ]
      }
    },

    propForList: {
      // searchs: {},

      title: rec => {
        return `${rec.display_name} ${rec.partner_id__name}`
      },
      icon: rec => {
        return ''
      },

      label: rec => {
        return `${rec.date_order || ''} ${rec.state__name || ''}`
      },
      value: rec => {
        return `金额: ${rec.amount_total || ''}`
      }
    }
  },

  extend: BaseClass => {
    class ModelClass extends BaseClass {
      global_domain(domain) {
        return super.global_domain(domain)
      }

      async create_by_template(template_id) {
        const res = await this.create()
        return res
      }

      async browse_one(rid, kwargs) {
        const res = await super.browse_one(rid, kwargs)

        return res
      }

      async search(params = {}) {
        const res = await super.search(params)
        return res
      }
    }
    return ModelClass
  }
}

const Models = {
  'sale.order': SaleOrder,
  'sale.order.line': SaleOrderLine
}

export default Models
